﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using Dapper;
using MySql.Data.MySqlClient;
using SHRMS.Models;

namespace SHRMS.childForm
{
    public partial class childForm_systemManage : Form
    {
        private readonly IDbConnection _dbConnection;
        public childForm_systemManage()
        {
            InitializeComponent();
            _dbConnection = new DbFactory().DefaultDbConnection;
            Init();
        }

        private void Init()
        {
            string sql = "SELECT role FROM `t_rolepower`";
            var result = _dbConnection.Query<string>(sql).ToList();
            comboBox_role.DataSource = result;
            comboBox_role.SelectedIndex = 0;
        }

        private void button_addAdmin_Click(object sender, EventArgs e)
        {
            string uname = textBox_uname.Text;
            string pwdtxt = textBox_pwd.Text;
            string email = textBox_email.Text;

            if (uname == "" || pwdtxt == "" || email == "")
            {
                MessageBox.Show("添加失败！请检查相关数据不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //MD5加密
                byte[] PwdByte = Encoding.Default.GetBytes(pwdtxt);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] output = md5.ComputeHash(PwdByte);
                string pwd = BitConverter.ToString(output).Replace("-", "");
                string sql_addStr = $"insert into t_user(userid,username,password,email,role) values('{Guid.NewGuid():N}','{uname}','{pwd}','{email}','管理员');";
                try
                {
                    var result = _dbConnection.Execute(sql_addStr);
                    if (result > 0)
                    {
                        MessageBox.Show($"添加管理员【{uname}】成功！", "添加成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        throw new Exception($"添加管理员【{uname}】失败！请检查相关数据合法性！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "添加失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void comboBox_role_SelectedIndexChanged(object sender, EventArgs e)
        {
            string role = comboBox_role.Text;
            string sql_selectStr = "select * from t_rolepower where role = @role";
            var result = _dbConnection.Query<Rolepower>(sql_selectStr, new { role = role }).FirstOrDefault();
            checkBox_dept.Checked = Convert.ToBoolean(result.Manage_Dept);
            checkBox_tea.Checked = Convert.ToBoolean(result.Manage_Teacher);
            checkBox_emp.Checked = Convert.ToBoolean(result.Manage_Emp);
            checkBox_atten.Checked = Convert.ToBoolean(result.Manage_Attendance);
            checkBox_salary.Checked = Convert.ToBoolean(result.Manage_Salary);
            checkBox_apply.Checked = Convert.ToBoolean(result.Apply);
            checkBox_info.Checked = Convert.ToBoolean(result.PersonalInfo);
        }

        private void button_done_Click(object sender, EventArgs e)
        {
            string role = comboBox_role.Text;
            if (role == "") { MessageBox.Show("抱歉，请选择你要授权的角色！", "未选择角色", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
            else
            {
                DialogResult res = MessageBox.Show($"您正在更改【{role}】的系统权限！\n请再次确认您要更改的权限是否合理！", "更改权限警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (res == DialogResult.Cancel)
                {
                    MessageBox.Show("修改权限已取消！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                int dept_s =Convert.ToInt32(checkBox_dept.Checked),
                     tea_s = Convert.ToInt32(checkBox_tea.Checked),
                     emp_s = Convert.ToInt32(checkBox_emp.Checked),
                     atten_s = Convert.ToInt32(checkBox_atten.Checked),
                     salary_s = Convert.ToInt32(checkBox_salary.Checked),
                     apply_s = Convert.ToInt32(checkBox_apply.Checked),
                     info_s = Convert.ToInt32(checkBox_info.Checked);

                string sql_update = $"update t_rolepower set manage_dept='{dept_s}',manage_teacher='{tea_s}',manage_emp='{emp_s}',manage_attendance='{atten_s}',manage_salary='{salary_s}',apply='{apply_s}',personalInfo='{info_s}' where role = '{role}';";
                try
                {
                    var result = _dbConnection.Execute(sql_update);
                    if (result > 0)
                    {
                        MessageBox.Show("修改权限成功！", "修改成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        throw new Exception("修改权限失败！未知错误！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
